﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Ccit.OA.NewsCenter
{
    public class CommentBLL
    {
        #region 初始化、释放对象

        IComment oComment = null;

        /// <summary>
        /// 创建对象
        /// </summary>
        private void CreateObject()
        {
            if (oComment == null)
                oComment = (IComment)Factory.CreateObject("Ccit.OA.NewsCenter.dll", "Ccit.OA.NewsCenter.Comment", ImportComponentType.DotNetDll, null);
        }

        /// <summary>
        /// 释放对象
        /// </summary>
        private void Dispose()
        {
            if (oComment != null)
                oComment = null;
        }

        #endregion

        /// <summary>
        /// 根据条件获得评论信息
        /// </summary>
        /// <param name="sNewsId">信息ID</param>
        /// <param name="sCommentContent">评论内容</param>
        /// <param name="sSortType">0：倒序(降序)；1：正序(升序)</param>
        /// <returns></returns>
        public List<MsComment> GetCommentByClause(string sNewsId, string sCommentContent, string sSortType)
        {
            CreateObject();
            List<MsComment> lstComment = oComment.GetCommentByClause(sNewsId,sCommentContent,sSortType);
            Dispose();
            return lstComment;
        }

        /// <summary>
        /// 操作评论
        /// </summary>
        /// <param name="sCommentIds">评论ID(当提交评论时为发表的评论信息)</param>
        /// <param name="sOperType">操作类型</param>
        /// <returns></returns>
        public bool OperateComment(string sCommentIds, string sOperType)
        {
            CreateObject();
            bool bFlag = false;
            if (sOperType == "Top")/* 设置评论置顶 */
            {
                bFlag = oComment.SetCommentTop(sCommentIds);
            }
            else if (sOperType == "CancelTop")/* 取消评论置顶 */
            {
                bFlag = oComment.CancelCommentTop(sCommentIds);
            }
            else if (sOperType == "Delete")/* 删除评论 */
            {
                bFlag = oComment.DeleteComment(sCommentIds);
            }
            else if (sOperType == "Comment")/* 发表(提交)评论 */
            {
                MsComment msComment = (MsComment)JavaScriptConvert.DeserializeObject(sCommentIds, typeof(MsComment));
                bFlag = oComment.ExpressComment(msComment);
            }
            return bFlag;
        }
    }
}
